package com.saoft.dbinfo;

import com.saoft.dbinfo.po.TableField;
import com.saoft.dbinfo.po.TableInfo;
import com.saoft.dbinfo.updates.MysqlUpdate;

import java.util.List;

/**
 * Created by saoft on 18/6/4.
 */
public class MysqlInfo {

                                                                                                                                                                                               
    public static void main(String[] args) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setDriverName("com.mysql.jdbc.Driver")
                .setUsername("root")
                .setPassword("root")
                .setUrl("jdbc:mysql://127.0.0.1:3306/hotel?characterEncoding=utf8");
        DbInfoBuilder dbInfoBuilder = new DbInfoBuilder(dataSourceConfig.getConn());
        List<TableInfo> tableInfoList = dbInfoBuilder.getTableInfoList();
        String tableName = "notice";
//        for (TableInfo tableInfo : tableInfoList) {
//            if(tableInfo.getName().equals(tableName)){
//                column(tableInfo);
//            }
//        }
//        for (TableInfo tableInfo : tableInfoList) {
//            MysqlUpdate mysqlUpdate = new MysqlUpdate();
//            String table = mysqlUpdate.createTable(tableInfo);
//            System.out.println(table);
//        }

        for (TableInfo tableInfo : tableInfoList) {
            markdown2(tableInfo);
        }
    }

    public static void column(TableInfo tableInfo){
        //{title: '日志名称', field: 'logname', align: 'center', valign: 'middle'},
        List<TableField> commonFields = tableInfo.getFields();
        for (TableField commonField : commonFields) {
            System.out.println("{title: '"+commonField.getComment()+"', field: '"+commonField.getName()+"', align: 'center', valign: 'middle'},");
        }
    }

    public static void markdown(TableInfo tableInfo){
        System.out.println("### "+tableInfo.getComment() + "表[" +tableInfo.getName()+"]");
        System.out.println("|字段名  | 类型 | 允许为空 | 说明 |");
        System.out.println("|--|--|--|--|");
        List<TableField> commonFields = tableInfo.getFields();
        for (TableField commonField : commonFields) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("|");
            buffer.append(commonField.getName());
            buffer.append("|");
            buffer.append(commonField.getType());
            buffer.append("|");
            buffer.append(commonField.getCustomMap().get("Null"));
            buffer.append("|");
            buffer.append(commonField.getComment());
            buffer.append("|");
            System.out.println(buffer.toString());
        }
    }

    public static void markdown2(TableInfo tableInfo){
        System.out.println("### "+tableInfo.getComment() + "表[" +tableInfo.getName()+"]");
        System.out.println("|字段名  | 类型 | 允许为空 | 默认值| 说明 |");
        System.out.println("|--|--|--|--|--|");
        List<TableField> commonFields = tableInfo.getFields();
        for (TableField commonField : commonFields) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("|");
            buffer.append(commonField.getName());
            buffer.append("|");
            buffer.append(commonField.getType());
            buffer.append("|");
            buffer.append(commonField.getCustomMap().get("Null"));
            buffer.append("|");
            buffer.append("");
            buffer.append("|");
            buffer.append(commonField.getComment());
            buffer.append("|");
            System.out.println(buffer.toString());
        }
    }
}
